﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

public partial class Admin_QuanLyHangHoa_SuaHangHoa : System.Web.UI.Page
{
    private string strMa = string.Empty;
    private DataAccess da = new DataAccess();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["AdminID"] == null)
        {
            Response.Write("<script language='javascript'>alert('Bạn phải đăng nhập / You must login');location.href='/../Admin/Login.aspx';</script>");
        }
        if (Request.QueryString["maHH"] != null && Request.QueryString["maHH"].ToString() != "")
        {
            strMa = Request.QueryString["maHH"].ToString();
        }
        if (!IsPostBack)
        {
            DDL_Nhomhang.Items.Clear();
            FillDropDownList();
            this.GetHanginfo(strMa);
        }
    }

    private void FillDropDownList()
    {
        SqlCommand comm1 = new SqlCommand("GetAllNhomHang", da.SqlConn());
        comm1.CommandType = CommandType.StoredProcedure;
        SqlDataReader drd1;
        comm1.Connection.Open();
        drd1 = comm1.ExecuteReader();
        while (drd1.Read())
        {
            ListItem ListNhom = new ListItem();
            ListNhom.Text = "" + drd1["TenNhomHang"].ToString();
            ListNhom.Value = drd1["MaNhomHang"].ToString();
            DDL_Nhomhang.Items.Add(ListNhom);
        }
        drd1.Close();
        comm1.Connection.Close();
        comm1.Connection.Dispose();
    }

    protected void GetHanginfo(string ma)
    {
        SqlDataReader drd;
        SqlCommand comm = new SqlCommand("getHangHoabyMa", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@Ma", ma);
        comm.Connection.Open();
        drd = comm.ExecuteReader();
        if (drd.Read())
        {
            this.DDL_Nhomhang.SelectedValue = drd["MaNhomHang"].ToString();           
            this.txtMa.Text = drd["MaHang"].ToString();
            this.txtTenHang.Text = drd["TenHang"].ToString();
            this.txtMota.Text = drd["Mota"].ToString();
            Image1.ImageUrl = drd["Images"].ToString();
            this.txtDonGia.Text = drd["DonGia"].ToString();
        }
        drd.Close();
        drd.Dispose();
        comm.Connection.Close();
        comm.Connection.Dispose();

    }
    protected void Hang_AddNew_Click(object sender, EventArgs e)
    {
        EditHangHoa();
    }

    private void EditHangHoa()
    {
        string strMaNhom = "";
        if (DDL_Nhomhang.SelectedValue.ToString() != "")
        {
            strMaNhom = DDL_Nhomhang.SelectedValue.ToString();
        }        
        string strMa = this.txtMa.Text;
        string strTenHang = this.txtTenHang.Text;
        string strMota = this.txtMota.Text;
        decimal dongia = Convert.ToDecimal(this.txtDonGia.Text);
        string ig = string.Empty;
        if (FileUpload1.HasFile)
        {
            
            FileUpload1.SaveAs(Server.MapPath("../../Images/Images_hanghoa/") + FileUpload1.FileName);
            ig = "../../Images/Images_hanghoa/" + FileUpload1.FileName;
        }
        else {
           ig = this.Image1.ImageUrl;
        }

        SqlCommand comm = new SqlCommand("updateHangHoa", da.SqlConn());
        comm.CommandType = CommandType.StoredProcedure;
        comm.Parameters.AddWithValue("@MaHang", strMa);
        comm.Parameters.AddWithValue("@MaNhom", strMaNhom);
        comm.Parameters.AddWithValue("@TenHang", strTenHang);
        comm.Parameters.AddWithValue("@Mota", strMota);
        comm.Parameters.AddWithValue("@Images", ig);
        comm.Parameters.AddWithValue("@DonGia", dongia);

        comm.Connection.Open();
        comm.ExecuteNonQuery();

        comm.Connection.Close();
        comm.Connection.Dispose();
        Response.Redirect("DanhSachHangHoa.aspx");
    }
}
